// pages/detail/detail.js
import {
  getDetail,
  getRecommend
} from '../../service/detail'

import {
  DetailBasicInfoClass,
  DetailShopInfoClass
} from './childCpns/detailClass'

import * as Constant from "../../common/constant";

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showNavBar: false,
    navBarHeight: 82 / 750 * wx.getSystemInfoSync().windowWidth,
    showBackToTop: false,
    navBarList: [],
    topImages: [],
    basicInfo: {},
    shopInfo: {},
    detailInfo: {},
    itemParams: {},
    recommendList: [],
    skuInfo: {},
    showSpecifications: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailNavBar: this.selectComponent("#w-detail-nav-bar"),
      query: wx.createSelectorQuery()
    })
    getDetail(options.iid).then(res => {
      let data = res.data;
      let commentInfo = {};
      if (data.rate.list) {
        commentInfo = data.rate.list[0];
      }
      this.setData({
        topImages: data.itemInfo.topImages,
        basicInfo: new DetailBasicInfoClass(data.itemInfo.iid, data.itemInfo.title, data.itemInfo.price, data.itemInfo.oldPrice, data.itemInfo.discountDesc, data.columns, data.shopInfo.services),
        shopInfo: new DetailShopInfoClass(data.shopInfo.shopLogo, data.shopInfo.name, data.shopInfo.cSells, data.shopInfo.cGoods, data.shopInfo.score),
        detailInfo: data.detailInfo,
        itemParams: data.itemParams,
        commentInfo: commentInfo,
        skuInfo: data.skuInfo
      })
    });
    getRecommend().then(res => {
      this.setData({
        recommendList: res.data.list
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 监听用户滑动页面事件
   */
  onPageScroll: function (options) {
    let currentIndex = this.data.detailNavBar.data.currentIndex;
    for (const [key, val] of this.data.navBarList.entries()) {
      if (options.scrollTop >= val) {
        currentIndex = 3 - key;
        break;
      }
    }
    if (currentIndex !== this.data.detailNavBar.data.currentIndex) {
      this.data.detailNavBar.setData({
        currentIndex
      })
    }
    let navBarFlag = options.scrollTop > 250;
    if (navBarFlag !== this.data.showNavBar) {
      this.setData({
        showNavBar: navBarFlag
      })
    }
    let backToTopFlag = options.scrollTop > Constant.showBackToTopDistance;
    if (backToTopFlag !== this.data.showBackToTop) {
      this.setData({
        showBackToTop: backToTopFlag
      })
    }
  },

  onImageLoad() {
    const query = this.data.query;
    let navBarList = this.data.navBarList;
    navBarList[3] = 0;
    query.select('#w-detail-params').boundingClientRect(res => {
      navBarList[2] = parseInt(res.top - this.data.navBarHeight);
    }).exec();
    query.select('#w-detail-evaluation').boundingClientRect(res => {
      navBarList[1] = parseInt(res.top - this.data.navBarHeight);
    }).exec();
    query.select('#w-recommend-info').boundingClientRect(res => {
      navBarList[0] = parseInt(res.top - this.data.navBarHeight);
    }).exec();
    this.setData({
      navBarList: navBarList
    })
  },

  handleNavBarClick(event) {
    wx.pageScrollTo({
      duration: 0,
      scrollTop: this.data.navBarList[3 - event.detail.currentIndex]
    })
  },

  handleAddToShopCart() {
    app.getUserProfile().then(res => {
      this.setData({
        showSpecifications: true
      })
    })
  },
  handleBackToTop() {
    wx.pageScrollTo({
      scrollTop: 0
    })
  }
})